package com.baitengsoft.patentservicemonitor.job.redis.service.impl;

import com.baitengsoft.patentservicemonitor.job.redis.service.IRedisListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wangze
 * @date 2018-08-26 16:19.
 **/
@Component
public class RedisListImpl implements IRedisListService {
    @Autowired
    private RedisTemplate<String, String> redis;

    /**
     * @param key
     * @param value
     * @description 简单的往数组里面添加元素
     */
    @Override
    public void lPush(String key, String value) {
        ListOperations<String, String> oper = redis.opsForList();
        int backValue = oper.leftPush(key, value).intValue();
        System.out.println("返回值 : " + backValue);
    }

    /**
     * @param key
     * @param list
     * @description 批量往数组里面添加元素
     */
    @Override
    public void lPushAll(String key, List<String> list) {
        ListOperations<String, String> oper = redis.opsForList();
        int backValue = oper.leftPushAll(key, list).intValue();
        System.out.println("返回值 : " + backValue);
    }

    /**
     * @param key
     * @param offset
     * @param value
     * @description 对指定下标的数组元素进行替换
     */
    @Override
    public void set(String key, int offset, String value) {
        ListOperations<String, String> oper = redis.opsForList();
        oper.set(key, offset, value);
    }

    /**
     * @param key
     * @param value1 原有值
     * @param value2 插入的值
     * @description 对指定下标的数组进行插入数据
     */
    @Override
    public void insert(String key, String value1, String value2) {
        ListOperations<String, String> oper = redis.opsForList();
        int backValue = oper.leftPush(key, value1, value2).intValue();
        System.out.println("返回值 : " + backValue);
    }

    /**
     * @param key
     * @param offset
     * @description 获取指定下标的数组元素
     */
    @Override
    public void getValueByIndex(String key, int offset) {
        ListOperations<String, String> oper = redis.opsForList();
        String value = oper.index(key, offset);
        System.out.println(offset + "位置下的值为 : " + value);
    }

    /**
     * @param key
     * @description 获取数组长度
     */
    @Override
    public Long size(String key) {
        ListOperations<String, String> oper = redis.opsForList();
        return oper.size(key);
    }

    /**
     * @param key
     * @param count 负数：从右往左     整数：从左往右
     * @param value 移除的值
     * @description 移除数组匹配到的数据元素
     */
    @Override
    public void remove(String key, long count, String value) {
        ListOperations<String, String> oper = redis.opsForList();
        int backValue = oper.remove(key, count, value).intValue();
        System.out.println(key + " 数组长度为 : " + backValue);
    }

    /**
     * @param key
     * @param start 区间开始
     * @param end   区间结束
     * @description 保留区间内的元素，区间外的全部删除
     */
    @Override
    public void trim(String key, int start, int end) {
        ListOperations<String, String> oper = redis.opsForList();
        oper.trim(key, start, end);
    }

    /**
     * @param key
     * @description 从左到右，删除第一个元素
     */
    public void lpop(String key) {
        ListOperations<String, String> oper = redis.opsForList();
        String value = oper.leftPop(key);
        System.err.println("移除的元素 ： " + value);
    }


    /**
     * @param key
     * @param start
     * @param end
     * @description 查询区间范围内的元素
     */
    @Override
    public List<String> lrange(String key, int start, int end) {
        ListOperations<String, String> oper = redis.opsForList();
        List<String> list = oper.range(key, start, end);
        return list;
    }

    /**
     * @param sourceKey      源
     * @param destinationKey 目的地
     * @description 两个list之间移元素
     */
    @Override
    public void rightPopAndLeftPush(String sourceKey, String destinationKey) {
        ListOperations<String, String> oper = redis.opsForList();
        String v = oper.rightPopAndLeftPush(sourceKey, destinationKey);
        System.out.println("----------" + v);
    }
}
